/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.dialog.ToastViewDialog;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.model.Schedule;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;

import java.util.List;

/**
 * FlaglayoutSlice
 *
 * @since 2021-03-29
 */
public class FlaglayoutSlice extends BaseAbilitySlice {
    private static final String[] SPIN_MUNE = {"点击空白格子试试~", "修改旗标布局背景", "重置旗标布局背景", "取消旗标布局", "恢复旗标布局"};

    TimetableView mTimetableView;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_flaglayout);
        initViews();
        showPopmenu();
        intents = intent;
    }

    /**
     * showPopmenu
     */
    public void showPopmenu() {
        MaterialSpinner spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        spinner.setOnItemSelectedListener((MaterialSpinner.OnItemSelectedListener<String>) (
                view, position, id, item) -> {
            selectLayout(position - 1);
            spinner.setShowFalse();
        });
        spinner.setItems(SPIN_MUNE);
        spinner.setOnNothingSelectedListener(spinner1 -> spinner1.setShowFalse());
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                modifyFlagBgcolor(new RgbColor(216, 30, 6));
                break;
            case 1:
                resetFlagBgcolor();
                break;
            case 2:
                cancelFlagBgcolor();
                break;
            case 3:
                resetFlaglayout();
                break;
            default:
                break;
        }
    }


    private void initViews() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .maxSlideItem(10)
                .callback(new ISchedule.OnItemClickListener() {
                    @Override
                    public void onItemClick(Component v, List<Schedule> scheduleList) {
                        display(scheduleList);
                    }
                })
                .callback(new ISchedule.OnItemLongClickListener() {
                    @Override
                    public void onLongClick(Component v, int day, int start) {
                        ToastViewDialog.toast(FlaglayoutSlice.this, "长按:周" + day + ",第" + start + "节");
                    }
                })
                .callback(new ISchedule.OnFlaglayoutClickListener() {
                    @Override
                    public void onFlaglayoutClick(int day, int start) {
                        FlaglayoutSlice.this.getUITaskDispatcher().asyncDispatch(() -> {
                            mTimetableView.hideFlaglayout();
                        });
                        ToastViewDialog.toast(FlaglayoutSlice.this, "点击了旗标:周" + (day + 1) + ",第" + start + "节");
                    }
                })
                .showView();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 显示内容
     *
     * @param beans
     */
    protected void display(List<Schedule> beans) {
        StringBuffer s = new StringBuffer();
        for (Schedule bean : beans) {
            s.append(bean.getName() + "、");
        }
        String str = s.toString();
        ToastViewDialog.toast(this, str);
    }

    /**
     * 修改旗标布局的背景色
     *
     * @param color
     */
    private void modifyFlagBgcolor(RgbColor color) {
        mTimetableView.flagBgcolor(color).updateFlaglayout();
    }

    /**
     * 重置旗标布局的背景色
     */
    private void resetFlagBgcolor() {
        mTimetableView.resetFlagBgcolor().updateFlaglayout();
    }

    /**
     * 取消旗标布局
     */
    private void cancelFlagBgcolor() {
        mTimetableView.isShowFlaglayout(false).updateFlaglayout();
    }

    /**
     * 显示旗标布局
     */
    private void resetFlaglayout() {
        mTimetableView.isShowFlaglayout(true).updateFlaglayout();
    }
}
